<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="录入时间" prop="registrationStarttime">
        <el-date-picker clearable
                        v-model="queryParams.registrationStarttime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择录入时间"
        >
        </el-date-picker>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">



      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['outpatient:registration:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getLists"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registrationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="流水ID" align="center" prop="registrationId"/>
      <el-table-column label="病患姓名" align="center" prop="patientName"/>
      <el-table-column label="性别" align="center" prop="dickerSex"/>

      <el-table-column label="年龄" align="center" prop="patientAge"/>
      <el-table-column label="联系方式" align="center" prop="doctorTel"/>

      <el-table-column label="录入时间" align="center" prop="registrationStarttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationStarttime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="registrationEndtime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationEndtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="挂号费用" align="center" prop="registrationMoney"/>
      <el-table-column label="医师姓名" align="center" prop="doctorName"/>
      <el-table-column label="科室名称" align="center" prop="deptName"/>


    </el-table>
    <h1 align="center">费用总计：{{moneyTotal}}</h1>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getLists"
    />


  </div>

</template>

<script>
import {
  getRegistration,
  delRegistration,
  addRegistration,
  updateRegistration,
  getdeptList,
  getDoctorList,
  listRegistrations,
  getDoctorLikeList
} from '@/api/outpatient/registration'
import item from '@/layout/components/Sidebar/Item.vue'

export default {
  name: 'Registration',
  data() {
    return {
      moneyTotal:0,
      prices:[],
      doctorId:'',
      doctorLists:[],


      value:'',






      //禁用
      disabled: true,

      deptDoct: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号流水表表格数据
      registrationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientName: null,
        dickerSex: null,
        patientIdcard: null,
        patientAge: null,
        doctorTel: null,
        doctorAdd: null,
        registrationStarttime: null,
        registrationEndtime: null,
        registrationMoney: null,
        doctorId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {

    this.getdepartmentlist()
    // this.getList()
    this.getLists()

    this.getDoctorLists()

  },
  methods: {
    deptLikeChange(id){
      this.queryParams.deptId = id
    },
    getDoctorLists(){
      getDoctorLikeList().then(response => {
        this.doctorLists = response.data
        this.loading = false
      })
    },
    doctorChange(doctorId){
      this.queryParams.doctorId = doctorId
    },
    sss(ss){
      this.form.state = ss
    },
    doctorWork(id){
      this.form.doctorId = id
    },

    getdepartmentlist() {
      getdeptList().then(response => {
        this.departmentlist = response.data.data
        this.loading = false
      })
    },
    changeset() {
      this.form.deptId = this.departmentid
      getDoctorList().then(response => {
        let data = response.data.data
        this.loading = false
        //如果部门id不为空
        if (this.departmentid) {
          //切换的时候人员清空，防止bug
          this.personlist = []
          //禁用取消
          this.disabled = false

          data.forEach(item => {

            if (item.departmentid == this.departmentid) {
              this.personlist.push(item)
            }
          })
        } else {
          //选中的部门为空，则清空人员列表
          this.personlist = []
          this.disabled = true

        }
      })
    },

    /** 查询挂号流水表列表 */
    getLists() {
      this.loading = true
      listRegistrations(this.queryParams).then(response => {
        this.registrationList = response.rows
        console.log(this.registrationList)
        this.total = response.total
        this.loading = false
      })
    },

    // /** 查询挂号流水表列表 */
    // getList() {
    //   this.loading = true
    //   listRegistration(this.queryParams).then(response => {
    //     this.registrationList = response.rows
    //     this.total = response.total
    //     this.loading = false
    //   })
    // },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.departmentid = null
      this.personid = null
    },
    // 表单重置
    reset() {
      this.form = {
        registrationId: null,
        patientName: null,
        dickerSex: null,
        patientIdcard: null,
        patientAge: null,
        doctorTel: null,
        doctorAdd: null,
        registrationStarttime: null,
        registrationEndtime: null,
        registrationMoney: null,
        doctorId: null,
        deptId: null,
        state:null



      }
      this.resetForm('form')
      this.value = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.doctorId=''
      this.departmentid=''
      this.getLists()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      this.moneyTotal=0;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.registrationId)
      this.prices = selection.map(item => item.registrationMoney)
       this.moneyTotal=0;
      for(let i=0;i<this.prices.length;i++){
        this.moneyTotal+=this.prices[i];
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增挂号表'
    },




    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const registrationId = row.registrationId || this.ids
      getRegistration(registrationId).then(response => {
        this.form = response.data
        this.value = response.data.state
        this.departmentid = this.form.deptId
        this.open = true
        this.title = '修改挂号表'
      })
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.registrationId != null) {

            updateRegistration(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.departmentid = null
              this.personid = null
              this.open = false
              this.getLists()
            })
          } else {
            addRegistration(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.departmentid = null
              this.personid = null
              this.open = false
              this.getLists()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const registrationIds = row.registrationId || this.ids
      this.$modal.confirm('是否确认删除挂号流水表编号为"' + registrationIds + '"的数据项？').then(function() {
        return delRegistration(registrationIds)
      }).then(() => {
        this.getLists()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('outpatient/registration/export', {
        ...this.queryParams
      }, `registration_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
